package cn.wen233.srpc.core.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;

/**
 * SPI 机制工具类
 *
 * @author wenei
 * @date 2021-12-25 23:43
 */
public class ServiceLoaderUtils {

    private static final Logger log = LoggerFactory.getLogger(ServiceLoaderUtils.class);

    public static <T> List<T> getLoaderClassInstance(Class<T> clazz) {
        List<T> result = new ArrayList<>();
        ServiceLoader<T> serviceLoader = ServiceLoader.load(clazz);
        for (T t : serviceLoader) {
            result.add(t);
            log.info("SPI注入Service={},Instance={}", clazz.getName(), t.getClass());
        }
        return result;
    }
}
